<?php
include_once("./_common.php");

/**
 * 두 좌표간의 거리를 구하기(WGS84 기준)
 * Get distance between coordinates in km
 * @param double $lat1 : 좌표1 위도
 * @param double $lon1 : 좌표1 경도
 * @param double $lat2 : 좌표2 위도
 * @param double $lon2 : 좌표2 경도
 * return double
 */
function get_distance($lat1, $lon1, $lat2, $lon2) {
    /* WGS84 stuff */
    $a = 6378137;
    $b = 6356752.3142;
    $f = 1/298.257223563;
    /* end of WGS84 stuff */

    $L = deg2rad($lon2-$lon1);
    $U1 = atan((1-$f) * tan(deg2rad($lat1)));
    $U2 = atan((1-$f) * tan(deg2rad($lat2)));
    $sinU1 = sin($U1);
    $cosU1 = cos($U1);
    $sinU2 = sin($U2);
    $cosU2 = cos($U2);

    $lambda = $L;
    $lambdaP = 2*pi();
    $iterLimit = 20;
    
    while ((abs($lambda-$lambdaP) > pow(10, -12)) && ($iterLimit-- > 0)) {
        $sinLambda = sin($lambda);
        $cosLambda = cos($lambda);
        $sinSigma = sqrt(($cosU2*$sinLambda) * ($cosU2*$sinLambda) 
                                        + ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda) * ($cosU1*$sinU2-$sinU1*$cosU2*$cosLambda));

        if ($sinSigma == 0) {
            return 0;
        }

        $cosSigma = $sinU1*$sinU2 + $cosU1*$cosU2*$cosLambda;
        $sigma = atan2($sinSigma, $cosSigma);
        $sinAlpha = $cosU1 * $cosU2 * $sinLambda / $sinSigma;
        $cosSqAlpha = 1 - $sinAlpha*$sinAlpha;
        $cos2SigmaM = $cosSigma - 2*$sinU1*$sinU2/$cosSqAlpha;

        if (is_nan($cos2SigmaM)) {
            $cos2SigmaM = 0;
        }

        $C = $f/16*$cosSqAlpha*(4+$f*(4-3*$cosSqAlpha));
        $lambdaP = $lambda;
        $lambda = $L + (1-$C) * $f * $sinAlpha * 
                            ($sigma + $C*$sinSigma*($cos2SigmaM+$C*$cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM)));
    }

    if ($iterLimit == 0) {
        // formula failed to converge
        return NaN;
    }

    $uSq = $cosSqAlpha * ($a*$a - $b*$b) / ($b*$b);
    $A = 1 + $uSq/16384*(4096+$uSq*(-768+$uSq*(320-175*$uSq)));
    $B = $uSq/1024 * (256+$uSq*(-128+$uSq*(74-47*$uSq)));
    $deltaSigma = $B*$sinSigma*($cos2SigmaM+$B/4*($cosSigma*(-1+2*$cos2SigmaM*$cos2SigmaM)
                                - $B/6*$cos2SigmaM*(-3+4*$sinSigma*$sinSigma)*(-3+4*$cos2SigmaM*$cos2SigmaM)));

    return round($b*$A*($sigma-$deltaSigma) / 1000);

    /* sphere way */
    $distance = rad2deg(acos(sin(deg2rad($lat1)) * sin(deg2rad($lat2))
                                        +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lon1 - $lon2))));

    $distance *= 111.18957696; // Convert to km

    return $distance;
}

$mta_this = "y".date("Y");

$sql = "SELECT a.store_num, a.a_name1, a.a_name2, a.a_addr1, a.a_tel, a.s_map_x, a.s_map_y, b.{$mta_this} as y_avg
          FROM manage a, month_avg b
         WHERE a.store_num = b.store_num
		 order by store_num ASC";
$rst = sql_query($sql);

while($row = mysql_fetch_assoc($rst))
{
	$list[] = $row;
}

$result = "";

for ($i=0; $i<count($list); $i++)
{
    $distance = get_distance($latitude, $longitude, $list[$i]['s_map_y'], $list[$i]['s_map_x']);

    if ($distance < 10) {
        $result .= $list[$i]['store_num']."<col>".$list[$i]['a_name1']."<col>".$list[$i]['a_name2']."<col>"
                        .$list[$i]['a_addr1']."<col>".$list[$i]['a_tel']."<col>".$list[$i]['s_map_x']."<col>".$list[$i]['s_map_y']."<col>"
                        .$list[$i]['y_avg']."<col>"."<col>END"."<row>";
    }
}

echo($result);
?>